
#! /usr/bin/python

import sys
import os
import time
#sys.path.append('..'+os.sep+'trunk')
sys.path.append('../../../../')
from spade import *
class Example(Agent.Agent):
     def _setup(self):
        self.setDefaultBehaviour(self.MainBehav())

     class MainBehav(Behaviour.OneShotBehaviour):


        def _process(self):
                print "Agent "+agent+" registering"
                lista= self.myAgent.getOrganizationList()
                print lista
                for i in lista:
                    info=self.myAgent.getOrganizationInfo(i)
                    if info.has_key("goal"):
                        goal=info["goal"]
                        g=eval(goal)
                        print info
                        if g.has_key("ofrece") and g["ofrece"]=="informacion turistica":
                            for i in xrange(0,1):
                                if info.has_key("manager"):
                                    manager=info["manager"]
                                    mens=ACLMessage.ACLMessage()
                                    mens.setPerformative("request")
                                    mens.setSender(self.myAgent.getAID())
                                    aid=AID.aid(manager,["xmpp://"+manager])
                                    mens.addReceiver(aid)
                                    mens.setOntology("Tourism")
                                    mens.setContent("Restaurant:Italiano")
                                    mens.setConversationId("eoeoe")
                                    print mens
                                    self.myAgent.send(mens)
                                    time.sleep(10)
                                    mens=Behaviour.ACLTemplate()
                                    mens.setPerformative("inform")
                                    mens.setOntology("Tourism")
                                    temp = Behaviour.MessageTemplate(mens)
                                    self.setTemplate(temp)
                                    ans=None
                                    ans=self._receive(True,10)
                                    while not ans or not temp.match(ans):
                                        ans=self._receive(True,10)
                                    print ans.getContent()
                                    time.sleep(10)
                                    #solicitud chino
                                    mens=ACLMessage.ACLMessage()
                                    mens.setPerformative("request")
                                    mens.setSender(self.myAgent.getAID())
                                    aid=AID.aid(manager,["xmpp://"+manager])
                                    mens.addReceiver(aid)
                                    mens.setOntology("Tourism")
                                    mens.setContent("Restaurant:Chino")
                                    mens.setConversationId("eoeoe")
                                    print mens
                                    self.myAgent.send(mens)
                                    time.sleep(10)
                                    mens=Behaviour.ACLTemplate()
                                    mens.setPerformative("inform")
                                    mens.setOntology("Tourism")
                                    temp = Behaviour.MessageTemplate(mens)
                                    self.setTemplate(temp)
                                    ans=None
                                    ans=self._receive(True,10)
                                    while not ans or not temp.match(ans):
                                        ans=self._receive(True,10)
                                    print ans.getContent()
                                    time.sleep(10)
                                    #solicitud museo
                                    mens=ACLMessage.ACLMessage()
                                    mens.setPerformative("request")
                                    mens.setSender(self.myAgent.getAID())
                                    aid=AID.aid(manager,["xmpp://"+manager])
                                    mens.addReceiver(aid)
                                    mens.setOntology("Tourism")
                                    mens.setContent("Museo:Arte Moderno")
                                    mens.setConversationId("eoeoe2")
                                    print mens
                                    self.myAgent.send(mens)
                                    time.sleep(10)
                                    mens=Behaviour.ACLTemplate()
                                    mens.setPerformative("inform")
                                    mens.setOntology("Tourism")
                                    temp = Behaviour.MessageTemplate(mens)
                                    self.setTemplate(temp)
                                    ans=None
                                    ans=self._receive(True,10)
                                    while not ans or not temp.match(ans):
                                        ans=self._receive(True,10)
                                    print ans.getContent()



if __name__ == "__main__":
    agent = "user@apolo.dsic.upv.es"
    b = Example(agent,"pasword")
    b.start()








